package guda.house.web.action.fee;

import guda.grape.util.RequestUtil;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.query.BankChargeRuleQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.biz.util.PriceUtil;
import guda.house.dal.dao.BankChargeRuleDAO;
import guda.house.dal.dataobject.BankChargeRuleDO;
import guda.house.dal.dataobject.SysVarDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by well on 15/11/20.
 */
@Controller
public class BankChargeCommonAction {

    @Autowired
    private BankChargeRuleDAO bankChargeRuleDAO;

    @RequestMapping(value = "fee/bankCharge/getBankCharge.htm")
    public void getBankCharge(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long varId = RequestUtil.getLong(request, "varId");
        long price = RequestUtil.getLong(request, "price");
        long villageId = RequestUtil.getLong(request, "villageId");
        JsonResult result = JsonResult.getInstance();
        if(varId ==0 || villageId ==0 ){
            result.setErrMsg("请先选择小区，以及要缴纳的费用");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        if(price ==0){
            result.getMap().put("total", 0);
            result.getMap().put("totalCN", 0);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

        BankChargeRuleQuery bankChargeRuleQuery = new BankChargeRuleQuery();
        bankChargeRuleQuery.setVillageId(villageId);
        bankChargeRuleQuery.setVarId(varId);


        List<BankChargeRuleDO> bankChargeRuleDOs = bankChargeRuleDAO.selectForPage(bankChargeRuleQuery);
        if(bankChargeRuleDOs.size() > 1){
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.RULE_MORE_THAN_ONE));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        if(bankChargeRuleDOs.size()  == 0){
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.RULE_NOT_FOUND));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        BankChargeRuleDO bankChargeRuleDO = bankChargeRuleDOs.get(0);
        Integer chargeUnit = bankChargeRuleDO.getChargeUnit();
        long total = price * chargeUnit /10000;
        if(bankChargeRuleDO.getMaxValue() != null){
            if(price > bankChargeRuleDO.getMaxValue().intValue()){
                total = Long.valueOf(bankChargeRuleDO.getMaxFee());
            }
        }

        result.getMap().put("total", total);
        result.getMap().put("totalCN", PriceUtil.formatCNY(total));
        ResponseHelper.ajaxReturn(result.getMap(), response);
        return;


    }
}
